package mdoc.tp1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import util.HibernateUtil;

import domain.Address;
import domain.Contact;
import domain.ContactGroup;
import domain.PhoneNumber;
import domain.DAO.DAOContact;
import domain.DAO.DAOContactGroup;
import domain.DAO.DAOFactory;
import domain.interfaces.IDAOContact;
import domain.interfaces.IDAOContactGroup;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if (request.getParameter("name").equals(request.getParameter("password")))
		{
			response.sendRedirect("LoadAllContacts");
		}
		else
		{
			response.sendRedirect("index.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	@Override
	public void init()
	{
		try
		{		
			IDAOContactGroup DAOCG = DAOFactory.getDAOContactGroup(getServletContext());
			if (DAOCG.findAll().size() == 0) {
				
				ContactGroup cg1 = new ContactGroup("Friends");
				ContactGroup cg2 = new ContactGroup("Colleague");
				ContactGroup cg3 = new ContactGroup("Acquaintance");
				ContactGroup cg4 = new ContactGroup("Family");

				DAOCG.create(cg1);
				DAOCG.create(cg2);
				DAOCG.create(cg3);
				DAOCG.create(cg4);
			}
			super.init();
		}
		catch (Exception e)
		{

		}
	}
}
